package com.lm.service;

import com.lm.mapper.LectureMapper;
import com.lm.pojo.Lecture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class LectureService {

    @Autowired
    private LectureMapper lectureMapper;

    @Autowired
    private CommentService commentService;

    public List<Lecture> getAllLectures() {
        return lectureMapper.selectAll();
    }

    public List<Lecture> getLecturesByCategory(String category) {
        if (category == null || category.isEmpty()) {
            return getAllLectures();
        }
        return lectureMapper.selectByCategory(category);
    }

    public List<Lecture> searchLecturesByTitle(String title) {
        if (title == null || title.trim().isEmpty()) {
            return getAllLectures();
        }
        return lectureMapper.searchByTitle("%" + title.trim() + "%");
    }

    @Transactional
    public void addLecture(Lecture lecture) {
        // 检查是否已存在相同的讲座
        List<Lecture> existingLectures = lectureMapper.selectAll();
        for (Lecture existing : existingLectures) {
            if (existing.getTitle().equals(lecture.getTitle()) &&
                existing.getTime().equals(lecture.getTime()) &&
                existing.getLocation().equals(lecture.getLocation())) {
                throw new RuntimeException("该讲座已存在");
            }
        }
        lectureMapper.insert(lecture);
    }

    @Transactional
    public void deleteLecture(Long id) {
        Lecture lecture = lectureMapper.selectByPrimaryKey(id);
        if (lecture == null) {
            throw new RuntimeException("讲座不存在");
        }
        
        // 删除讲座相关的预约记录
        lectureMapper.deleteAllBookings(id);
        
        // 删除讲座相关的评论
        commentService.deleteCommentsByLectureId(id);
        
        // 删除讲座
        int result = lectureMapper.deleteByPrimaryKey(id);
        if (result != 1) {
            throw new RuntimeException("删除讲座失败");
        }
    }

    @Transactional
    public void bookLecture(Long lectureId, String nickname) {
        // 检查讲座是否存在
        Lecture lecture = lectureMapper.selectByPrimaryKey(lectureId);
        if (lecture == null) {
            throw new RuntimeException("讲座不存在");
        }
        
        // 检查讲座时间是否已过
        if (lecture.getTime().before(new Date())) {
            throw new RuntimeException("讲座时间已过，无法预约");
        }
        
        // 检查是否已经预约
        if (lectureMapper.checkBooking(lectureId, nickname)) {
            throw new RuntimeException("您已经预约过这个讲座了");
        }
        
        // 添加预约记录
        lectureMapper.addBooking(lectureId, nickname);
        
        // 更新讲座的预约人数
        lecture.setAttendeeCount(lecture.getAttendeeCount() + 1);
        lectureMapper.updateByPrimaryKey(lecture);
    }

    public List<Lecture> getUserBookings(String nickname) {
        return lectureMapper.getUserBookings(nickname);
    }

    public void cancelBooking(Long lectureId, String nickname) {
        // 检查是否已经预约
        if (!lectureMapper.checkBooking(lectureId, nickname)) {
            throw new RuntimeException("您还没有预约这个讲座");
        }
        
        // 删除预约记录
        lectureMapper.deleteBooking(lectureId, nickname);
    }

    public Lecture getLectureById(Long id) {
        return lectureMapper.selectByPrimaryKey(id);
    }

    public void saveLecture(Lecture lecture) {
        lectureMapper.insert(lecture);
    }

    public boolean isPublisher(Long lectureId, String uid) {
        Lecture lecture = getLectureById(lectureId);
        return lecture != null && lecture.getPublisherUid().equals(uid);
    }
}